1 using System;
2 using
System.Collections.Generic;
3 using
System.ComponentModel;
4 using
System.Drawing;
5 using
System.Data;
6 using
System.Linq;
7 using
System.Text;
8 using
System.Threading.Tasks;
9 using
System.Windows.Forms;
10 using
System.Globalization;
11 using
System.Threading;
12
13 namespace
SoftQuanLyNhaHang.Views
14 {
15     
public delegate void Link_QuayLai_EventHandler(object sender, EventArgs e);
16
17     
public partial class uctBom_Form : UserControl
18     {
19         
20
21         
public uctBom_Form()
22         {
23             InitializeComponent();
24         }
25
26
27         
public static uctBom_Form uctBomF = new uctBom_Form();
28
29         
public event Link_QuayLai_EventHandler Link_QuayLai;
30
31         
private int _getIdBom = 0;
32         
private int _getIdBomItem = 0;
33
34         
//bom
35         
//int _IdBom = 0;
36         
string _TieuDe = string.Empty;
37         
float _SoLuongGoi = 0;
38         DateTime _NgayBatDau = DateTime.Now;
39         DateTime _NgayKetThuc = DateTime.Now;
40         DateTime _NgayTao = DateTime.Now;
41         
double _GiaVonBom = 0;
42         
double _GiaBanBom = 0;
43         
string _TrangThai = "SuDung";
44         
string _MoTa = string.Empty;
45
46         
bool nhanEnter = false;
47
48         
//bom item
49         
//int _IdBomItem = 0;
50         
//int _IdBom = 0;
51         
int _IdKho = 0;
52         
string _ChuanMaVach = string.Empty;
53         
//string _TrangThaiSanPham = "MuaHang";
54         
string _LoaiHangHoa = "MuaHang";
55         
double _GiaBanSp = 0;
56         
float _SoLuongSp = 0;
57         
string _TenSanPham = string.Empty;
58         
int _DonViTinhId = 0;
59         
string _TenDonViTinh = string.Empty;
60
61
62         
private void btnClear_Click(object sender, EventArgs e)
63         {
64             
this.ClearAllFormSp();
65         }
66
67         
private void ClearAllFormSp()
68         {
69             txtChuanMaVach.Text =
"";
70             txtTenSp.Text =
"";
71             txtSoLuongSp.Text =
"";
72             txtGiaBan.Text =
"";
73             txtGiaVon.Text =
"";
74             _getIdBomItem =
0;
75
76             
//lblThongBao.Visible = false;
77         }
78
79         
private void ClearInforFormSp()
80         {
81             txtChuanMaVach.Text =
string.Empty;
82             txtSoLuongSp.Text =
"";
83             txtGiaBan.Text =
"";
84         }
85
86         
private void btnLuu_Click(object sender, EventArgs e)
87         {
88             
//Them moi bom
89             
this.InsertUpdatBom();
90
91             
try
92             {
93                 _ChuanMaVach = txtChuanMaVach.Text.Trim();
94             }
95             
catch { }
96
97             
/*
98             
if (string.IsNullOrEmpty(_ChuanMaVach))
99             {
100                 MessageBox.Show(
"Chuẩn mã vạch không được để trống");
101
102                 txtChuanMaVach.Focus();
103                 
return;
104             }
105             */

106             
try
107             {
108                 _TenSanPham = txtTenSp.Text.Trim();
109             }
110             
catch { }
111
112             
try
113             {
114                 _SoLuongSp = (
float)CauHinhHeThong.Format_SoDeTinhToan(txtSoLuongSp.Text.Trim(),true);
115             }
116             
catch { }
117
118             
if (rdoMuaHang.Checked == true)
119             {
120                 _LoaiHangHoa =
"MuaHang";
121             }
122             
else
123             {
124                 _LoaiHangHoa =
"HangKhuyenMai";
125             }
126
127
128             
//enter san pham
129
130             
/*
131             
if (string.IsNullOrEmpty(txtSoLuongSp.Text.Trim()))
132             {
133                 MessageBox.Show(
"Số lượng không được để trống");
134
135                 txtSoLuongSp.Focus();
136                 
return;
137             }
138             */

139             
/*
140             
if (string.IsNullOrEmpty(txtGiaBan.Text.Trim()))
141             {
142                 MessageBox.Show(
"Giá bán không được để trống");
143
144                 txtGiaBan.Focus();
145                 
return;
146             }
147             */

148             
try
149             {
150                 _GiaBanSp = CauHinhHeThong.Format_SoDeTinhToan(txtGiaBan.Text.Trim(),
true);
151             }
152             
catch { }
153
154             
if (!string.IsNullOrEmpty(_ChuanMaVach) && !string.IsNullOrEmpty(txtSoLuongSp.Text.Trim()))
155             {
156                 
if (_getIdBomItem == 0)
157                 {
158                     _getIdBomItem = Controllers.BomItemCtrl.InsertBomItem(_getIdBomItem, _getIdBom, _IdKho, _ChuanMaVach, _TrangThai, _LoaiHangHoa, _GiaBanSp, _SoLuongSp, _TenSanPham, _DonViTinhId, _TenDonViTinh);
159                 }
160                 
else
161                 {
162                     Controllers.BomItemCtrl.UpdateBomItem(_getIdBomItem, _getIdBom, _IdKho, _ChuanMaVach, _TrangThai, _LoaiHangHoa, _GiaBanSp, _SoLuongSp, _TenSanPham, _DonViTinhId, _TenDonViTinh);
163                 }
164             }
165
166             
//Hiển thị xuống danh sách sản phẩm
167             
this.HienThiDanhSachMuaHang(_getIdBom);
168             
//this.HienThiDanhSachKhuyenMai(_getIdBom);
169
170             lblThongBao.Text =
"Lưu thành công!";
171             lblThongBao.Visible =
true;
172
173             nhanEnter =
false;
174             
//ẩn thông báo sau 3 giây
175             
var t = new System.Windows.Forms.Timer();
176             t.Interval =
3000; // it will Tick in 3 seconds
177             t.Tick += (s, ex) =>
178             {
179                 lblThongBao.Visible =
false;
180
181                 
if (nhanEnter == false)
182                 {
183                     
this.ClearAllFormSp();
184                     txtChuanMaVach.Focus();
185                 }
186
187                 t.Stop();
188             };
189             t.Start();
190         }
191
192         
private void btnHuy_Click(object sender, EventArgs e)
193         {
194
195         }
196
197         
private void btnThucHien_Click(object sender, EventArgs e)
198         {
199             
if (string.IsNullOrEmpty(txtTieuDe.Text.Trim()))
200             {
201                 MessageBox.Show(
"Tiêu đề không được để trống");
202                 txtTieuDe.Focus();
203                 
return;
204             }
205
206             
if (string.IsNullOrEmpty(txtSoLuongGoi.Text.Trim()))
207             {
208                 MessageBox.Show(
"Số gói không được để trống");
209                 txtSoLuongGoi.Focus();
210                 
return;
211             }
212
213             
//thêm mới bom
214             
this.InsertUpdatBom();
215
216             MessageBox.Show(
"Thêm gói sản phẩm thành công!");
217         }
218
219         
private void InsertUpdatBom()
220         {
221             
try
222             {
223                 _TieuDe = txtTieuDe.Text.Trim();
224             }
225             
catch { }
226
227             
try
228             {
229                 
//_NgayBatDau = DateTime.ParseExact();// DateTime.ParseExact(dateNgayBatDau.Text, "yyyy-MM-dd", CultureInfo.InvariantCulture);
230                 _NgayBatDau = dateNgayBatDau.Value;
231             }
232             
catch { }
233
234             
try
235             {
236                 _NgayKetThuc = dateNgayKetThuc.Value; ;
// DateTime.ParseExact(dateNgayKetThuc.Text, "yyyy-MM-dd", CultureInfo.InvariantCulture);
237             }
238             
catch { }
239
240             
try
241             {
242                 _SoLuongGoi = (
float)CauHinhHeThong.Format_SoDeTinhToan(txtSoLuongGoi.Text.Trim(),true);
243             }
244             
catch { }
245
246             
try
247             {
248                 _MoTa = txtMoTa.Text.Trim();
249             }
250             
catch { }
251
252             
if (_getIdBom == 0)
253             {
254                 _getIdBom = Controllers.BomCtrl.InsertBom(_getIdBom, _TieuDe, _SoLuongGoi, _NgayBatDau, _NgayKetThuc, _GiaVonBom, _GiaBanBom, _TrangThai, _MoTa,_NgayTao);
255             }
256             
else
257             {
258                 Controllers.BomCtrl.UpdateBom(_getIdBom, _TieuDe, _SoLuongGoi, _NgayBatDau, _NgayKetThuc, _GiaVonBom, _GiaBanBom, _TrangThai, _MoTa, _NgayTao);
259             }
260
261             
262
263         }
264
265         
private void txtSoLuongGoi_TextChanged(object sender, EventArgs e)
266         {
267             
try
268             {
269                 
string value = txtSoLuongGoi.Text.Replace(CauHinhHeThong.PHAN_CACH_HANG_NGHIN, "");
270                 
ulong ul = 0;
271
272                 
if (ulong.TryParse(value, out ul))
273                 {
274                     txtSoLuongGoi.TextChanged -= txtSoLuongGoi_TextChanged;
275
276                     
if (txtSoLuongGoi.Text.StartsWith("0"))
277                     {
278                         txtSoLuongGoi.Text = ul.ToString();
279                     }
280                     
else
281                     {
282                         txtSoLuongGoi.Text = CauHinhHeThong.DoFormat_NhapSoTrongText(
value);
283                     }
284
285                     txtSoLuongGoi.SelectionStart = txtSoLuongGoi.Text.Length;
286                     txtSoLuongGoi.TextChanged += txtSoLuongGoi_TextChanged;
287                 }
288                 
else
289                 {
290                     
if (txtSoLuongGoi.Text.StartsWith(CauHinhHeThong.DAU_PHAY))
291                     {
292                         txtSoLuongGoi.TextChanged -= txtSoLuongGoi_TextChanged;
293                         txtSoLuongGoi.Text =
"0" + txtSoLuongGoi.Text;
294                         txtSoLuongGoi.SelectionStart = txtSoLuongGoi.Text.Length;
295                         txtSoLuongGoi.TextChanged += txtSoLuongGoi_TextChanged;
296                     }
297                 }
298             }
299             
catch
300             {
301             }
302         }
303
304         
private void txtSoLuongSp_TextChanged(object sender, EventArgs e)
305         {
306             
try
307             {
308                 
string value = txtSoLuongSp.Text.Replace(CauHinhHeThong.PHAN_CACH_HANG_NGHIN, "");
309                 
ulong ul = 0;
310
311                 
if (ulong.TryParse(value, out ul))
312                 {
313                     txtSoLuongSp.TextChanged -= txtSoLuongSp_TextChanged;
314
315                     
if (txtSoLuongSp.Text.StartsWith("0"))
316                     {
317                         txtSoLuongSp.Text = ul.ToString();
318                     }
319                     
else
320                     {
321                         txtSoLuongSp.Text = CauHinhHeThong.DoFormat_NhapSoTrongText(
value);
322                     }
323
324                     txtSoLuongSp.SelectionStart = txtSoLuongSp.Text.Length;
325                     txtSoLuongSp.TextChanged += txtSoLuongSp_TextChanged;
326                 }
327                 
else
328                 {
329                     
if (txtSoLuongSp.Text.StartsWith(CauHinhHeThong.DAU_PHAY))
330                     {
331                         txtSoLuongSp.TextChanged -= txtSoLuongSp_TextChanged;
332                         txtSoLuongSp.Text =
"0" + txtSoLuongSp.Text;
333                         txtSoLuongSp.SelectionStart = txtSoLuongSp.Text.Length;
334                         txtSoLuongSp.TextChanged += txtSoLuongSp_TextChanged;
335                     }
336                 }
337             }
338             
catch
339             {
340             }
341         }
342
343         
private void txtGiaBan_TextChanged(object sender, EventArgs e)
344         {
345             
try
346             {
347                 
string value = txtGiaBan.Text.Replace(CauHinhHeThong.PHAN_CACH_HANG_NGHIN, "");
348                 
ulong ul = 0;
349
350                 
if (ulong.TryParse(value, out ul))
351                 {
352                     txtGiaBan.TextChanged -= txtGiaBan_TextChanged;
353
354                     
if (txtGiaBan.Text.StartsWith("0"))
355                     {
356                         txtGiaBan.Text = ul.ToString();
357                     }
358                     
else
359                     {
360                         txtGiaBan.Text = CauHinhHeThong.DoFormat_NhapSoTrongText(
value);
361                     }
362
363                     txtGiaBan.SelectionStart = txtGiaBan.Text.Length;
364                     txtGiaBan.TextChanged += txtGiaBan_TextChanged;
365                 }
366                 
else
367                 {
368                     
if (txtGiaBan.Text.StartsWith(CauHinhHeThong.DAU_PHAY))
369                     {
370                         txtGiaBan.TextChanged -= txtGiaBan_TextChanged;
371                         txtGiaBan.Text =
"0" + txtGiaBan.Text;
372                         txtGiaBan.SelectionStart = txtGiaBan.Text.Length;
373                         txtGiaBan.TextChanged += txtGiaBan_TextChanged;
374                     }
375                 }
376             }
377             
catch
378             {
379             }
380         }
381
382         
private void txtSoLuongGoi_KeyPress(object sender, KeyPressEventArgs e)
383         {
384             CauHinhHeThong.NhapSoTrongTextBox(sender, e);
385         }
386
387         
private void txtSoLuongMua_KeyPress(object sender, KeyPressEventArgs e)
388         {
389             CauHinhHeThong.NhapSoTrongTextBox(sender, e);
390         }
391
392         
private void txtGiaBan_KeyPress(object sender, KeyPressEventArgs e)
393         {
394             CauHinhHeThong.NhapSoTrongTextBox(sender, e);
395         }
396
397         
private void txtChuanMaVach_KeyDown(object sender, KeyEventArgs e)
398         {
399             
if (e.KeyCode == Keys.Enter)
400             {
401                 nhanEnter =
true;
402                 
//Ấn enter không có tiếng kêu
403                 e.SuppressKeyPress =
true;
404                 
//btnLuu_Click(this, new EventArgs());
405
406                 
//lblThongBao.Visible = false;
407
408                 
try
409                 {
410                     _ChuanMaVach = txtChuanMaVach.Text.Trim();
411                 }
412                 
catch { }
413
414                 
if (string.IsNullOrEmpty(_ChuanMaVach))
415                 {
416                     MessageBox.Show(
"Chuẩn mã vạch không được để trống");
417
418                     txtChuanMaVach.Focus();
419                     
return;
420                 }
421
422                 
if (rdoMuaHang.Checked == true)
423                 {
424                     _LoaiHangHoa =
"MuaHang";
425                 }
426                 
else
427                 {
428                     _LoaiHangHoa =
"HangKhuyenMai";
429                 }
430
431                 
if (_getIdBom == 0)
432                 {
433                     
this.InsertUpdatBom();
434                 }
435                 
//lay trong bang bomItem
436                 DataTable dtBomItem =
new DataTable();
437
438                 
//dtBomItem = Controllers.BomItemCtrl.FillDataSet_getBomItem_SearchAll(_getIdBomItem, _getIdBom, _IdKho, _ChuanMaVach, _TrangThai, _LoaiHangHoa, _GiaBanSp, _SoLuongSp, _TenSanPham, _DonViTinhId, _TenDonViTinh).Tables[0];
439                 dtBomItem = Controllers.BomItemCtrl.FillDataSet_getBomItem_ByLoaiHangHoa_ChuanMaVach(_getIdBom,_LoaiHangHoa, _ChuanMaVach).Tables[
0];
440
441                 
if (dtBomItem.Rows.Count > 0)
442                 {
443                     
//Tìm thấy sản phẩm trong bảng bomItem
444                     _TenSanPham = dtBomItem.Rows[
0]["TenSanPham"].ToString();
445
446                     _TenDonViTinh = dtBomItem.Rows[
0]["TenDonViTinh"].ToString();
447                     _DonViTinhId = Int32.Parse(dtBomItem.Rows[
0]["DonViTinhId"].ToString());
448
449                     _IdKho = Int32.Parse(dtBomItem.Rows[
0]["IdKho"].ToString());
450
451                     _ChuanMaVach = dtBomItem.Rows[
0]["ChuanMaVach"].ToString();
452
453                     
//_SoLuongSp = float.Parse(CauHinhHeThong.Format_SoDeTinhToan(dtBomItem.Rows[0]["SoLuong"].ToString()).ToString());
454
455                     
double soluongGetInSql = 0;
456                     soluongGetInSql = CauHinhHeThong.Format_SoDeTinhToan(dtBomItem.Rows[
0]["SoLuong"].ToString(), false);
457
458                     
double soluongtxt = 0;
459                     soluongtxt = CauHinhHeThong.Format_SoDeTinhToan(txtSoLuongSp.Text.Trim(),
true);
460
461                     
if (soluongtxt == soluongGetInSql)
462                     {
463                         
//so luong tang them 1
464                         _SoLuongSp =
float.Parse((soluongGetInSql + 1).ToString());
465                     }
466                     
else
467                     {
468                         _SoLuongSp = (
float)CauHinhHeThong.Format_SoDeTinhToan(txtSoLuongSp.Text.Trim(), true);
469                     }
470
471                     
double giabantxt = 0;
472                     giabantxt = CauHinhHeThong.Format_SoDeTinhToan(txtGiaBan.Text.Trim(),
true);
473
474                     
double giabanGetInSql = 0;
475                     giabanGetInSql = CauHinhHeThong.Format_SoDeTinhToan(dtBomItem.Rows[
0]["GiaBan"].ToString(), false);
476
477
478                     
if (giabantxt == giabanGetInSql)
479                     {
480                         _GiaBanSp = giabanGetInSql;
// float.Parse(CauHinhHeThong.Format_SoDeTinhToan(txtGiaBan.Text.Trim()).ToString());
481                     }
482                     
else
483                     {
484                         _GiaBanSp = (
float)CauHinhHeThong.Format_SoDeTinhToan(txtGiaBan.Text.Trim(), true);
485                     }
486
487                     txtSoLuongSp.Text = CauHinhHeThong.Format_SoDeHienThi(_SoLuongSp,
false);
488                     txtGiaBan.Text = CauHinhHeThong.Format_SoDeHienThi(_GiaBanSp,
true);
489                     txtTenSp.Text = _TenSanPham;
490
491                     _getIdBomItem = Int32.Parse(dtBomItem.Rows[
0]["IdBomItem"].ToString());
492
493                     
//cho phep lưu
494                     btnLuu.Enabled =
true;
495                 }
496                 
else
497                 {
498                     
//thêm mới sp vào bảng bomItem
499                     _getIdBomItem =
0;
500                     
//Lấy sản phẩm trong kho
501                     DataTable dtKho =
new DataTable();
502
503                     dtKho = Controllers.KhoCtrl.FillDataSet_getKho_ByMaSanPham_OrMaVietTat(_ChuanMaVach).Tables[
0];
504
505                     
//kiểm tra có tìm thấy trong bảng nhập kho hay không?
506                     
if (dtKho.Rows.Count > 0)
507                     {
508                         
//Tìm thấy sản phẩm trong bảng kho
509                         _TenSanPham = dtKho.Rows[
0]["TenSanPham"].ToString();
510                         _GiaBanSp = CauHinhHeThong.Format_SoDeTinhToan(dtKho.Rows[
0]["GiaBanLe"].ToString(),false);
511                         
//_GiaVonBom = CauHinhHeThong.Format_SoDeTinhToan(dtKho.Rows[0]["GiaNhapMotSp"].ToString(), false);
512                         _TenDonViTinh = dtKho.Rows[
0]["TenDonViTinh"].ToString();
513                         _DonViTinhId = Int32.Parse(dtKho.Rows[
0]["DonViTinhId"].ToString());
514
515                         _IdKho = Int32.Parse(dtKho.Rows[
0]["IdKho"].ToString());
516
517                         _ChuanMaVach = dtKho.Rows[
0]["MaVietTat"].ToString();
518
519                         
/*
520                         
if (!string.IsNullOrEmpty(dtKho.Rows[0]["MaVietTat"].ToString()))
521                         {
522                             _ChuanMaVach = dtKho.Rows[
0]["MaVietTat"].ToString();
523                         }
524                         
else
525                         {
526                             _ChuanMaVach = dtKho.Rows[
0]["MaSanPham"].ToString();
527                         }
528                         */

529                         _SoLuongSp =
1;// float.Parse(CauHinhHeThong.Format_SoDeTinhToan(txtSoLuongSp.Text.Trim()).ToString());
530
531                         txtSoLuongSp.Text =
"1";
532                         txtGiaBan.Text = CauHinhHeThong.Format_SoDeHienThi(_GiaBanSp,
true);
533                         txtTenSp.Text = _TenSanPham;
534                         
//cho phep lưu
535                         btnLuu.Enabled =
true;
536                     }
537                     
else
538                     {
539                         DataTable dtTtsp =
new DataTable();
540
541                         dtTtsp = Controllers.ThongTinSanPhamCtrl.FillDataSet_ThongTinSanPhamByMaSanPham(txtChuanMaVach.Text.Trim()).Tables[
0];
542
543                         
if (dtTtsp.Rows.Count > 0)
544                         {
545                             
//Tìm thấy sản phẩm trong bảng kho
546                             _TenSanPham = dtTtsp.Rows[
0]["TenSanPham"].ToString();
547                             _GiaBanSp = CauHinhHeThong.Format_SoDeTinhToan(dtTtsp.Rows[
0]["TenNhaCungCap"].ToString(), false);
548                             _TenDonViTinh = dtTtsp.Rows[
0]["TenDonViTinh"].ToString();
549                             _DonViTinhId = Int32.Parse(dtTtsp.Rows[
0]["DonViTinhId"].ToString());
550
551                             _IdKho =
0;// Int32.Parse(dtKho.Rows[0]["IdKho"].ToString());
552
553                             _ChuanMaVach = txtChuanMaVach.Text.Trim();
554
555                             _SoLuongSp =
1;
556
557                             txtSoLuongSp.Text =
"1";
558                             txtGiaBan.Text = CauHinhHeThong.Format_SoDeHienThi(_GiaBanSp,
true);
559                             txtTenSp.Text = _TenSanPham;
560                             
//cho phep lưu
561                             btnLuu.Enabled =
true;
562                         }
563                         
else
564                         {
565                             txtTenSp.Text =
"";
566                             txtGiaBan.Text =
"";
567                             txtSoLuongSp.Text =
"";
568
569                         }
570
571                         
//MessageBox.Show("Sản phẩm không tồn tại trong kho");
572                         
//txtChuanMaVach.Focus();
573                         
//không cho lưu
574                         
//btnLuu.Enabled = false;
575                         
//return;
576                     }
577                 }
578             }
579         }
580
581
582         
private void HienThiDanhSachMuaHang(int getIdBom)
583         {
584             dgvDsMuaHang.Rows.Clear();
585             
//Ẩn cột đầu tiên
586             dgvDsMuaHang.RowHeadersVisible =
false;
587             
//-https://stackoverflow.com/questions/17404969/right-align-a-column-in-datagridview-doesnt-work
588             
this.dgvDsMuaHang.Columns["STTMuaHang"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
589             
this.dgvDsMuaHang.Columns["STTMuaHang"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
590             
//this.dgvDanhSachNhapHang.Columns["STT"].SortMode = DataGridViewColumnSortMode.NotSortable;
591
592             
this.dgvDsMuaHang.Columns["TenDonViTinhMuaHang"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
593             
this.dgvDsMuaHang.Columns["TenDonViTinhMuaHang"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
594
595             
this.dgvDsMuaHang.Columns["SoLuongMuaHang"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
596             
this.dgvDsMuaHang.Columns["SoLuongMuaHang"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
597
598             
this.dgvDsMuaHang.Columns["GiaVonMuaHang"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
599             
this.dgvDsMuaHang.Columns["GiaVonMuaHang"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
600
601             
this.dgvDsMuaHang.Columns["GiaBanMuaHang"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
602             
this.dgvDsMuaHang.Columns["GiaBanMuaHang"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
603
604             
//this.dgvDsMuaHang.Columns["ThanhTien"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
605             
this.dgvDsMuaHang.Columns["ThanhTien"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
606
607
608             
this.dgvDsMuaHang.Columns["TrangThai"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
609             
this.dgvDsMuaHang.Columns["TrangThai"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
610
611             
this.dgvDsMuaHang.Columns["Sua"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
612             
this.dgvDsMuaHang.Columns["Sua"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
613
614
615             DataTable dtBomItem =
new DataTable();
616
617             
//select từ bảng bán hàng
618             dtBomItem = Controllers.BomItemCtrl.FillDataSet_getBomItem_SearchAll(_getIdBomItem, getIdBom, _IdKho,
"", _TrangThai, "", _GiaBanSp, _SoLuongSp, _TenSanPham, _DonViTinhId, _TenDonViTinh).Tables[0];
619             
int _stt = 0;
620             
double soluong = 0;
621             
double giaban = 0;
622             
double giavon = 0;
623             
double thanhT = 0;
624
625             
if (dtBomItem.Rows.Count > 0)
626             {
627                 
string trangThai = "";
628
629                 
foreach (DataRow item in dtBomItem.Rows)
630                 {
631                     _stt +=
1;
632                     
int n = dgvDsMuaHang.Rows.Add();
633
634                     dgvDsMuaHang.Rows[n].Cells[
"IdBomItemMuaHang"].Value = item["IdBomItem"].ToString();
635                     dgvDsMuaHang.Rows[n].Cells[
"STTMuaHang"].Value = _stt.ToString();
636                     dgvDsMuaHang.Rows[n].Cells[
"ChuanMaVachMuaHang"].Value = item["ChuanMaVach"].ToString();
637                     dgvDsMuaHang.Rows[n].Cells[
"TenSanPhamMuaHang"].Value = item["TenSanPham"].ToString();
638
639                     dgvDsMuaHang.Rows[n].Cells[
"TenDonViTinhMuaHang"].Value = item["TenDonViTinh"].ToString();
640                     dgvDsMuaHang.Rows[n].Cells[
"DonViTinhId"].Value = item["DonViTinhId"].ToString();
641
642                     soluong = CauHinhHeThong.Format_SoDeTinhToan(item[
"SoLuong"].ToString(),false);
643                     dgvDsMuaHang.Rows[n].Cells[
"SoLuongMuaHang"].Value = CauHinhHeThong.Format_SoDeHienThi(soluong, false) ;
644
645                     giavon = CauHinhHeThong.Format_SoDeTinhToan(item[
"GiaNhap"].ToString(), false);
646                     dgvDsMuaHang.Rows[n].Cells[
"GiaVonMuaHang"].Value =CauHinhHeThong.Format_SoDeHienThi(giavon, true) ;
647
648                     giaban = CauHinhHeThong.Format_SoDeTinhToan(item[
"GiaBan"].ToString(), false);
649                     dgvDsMuaHang.Rows[n].Cells[
"GiaBanMuaHang"].Value =CauHinhHeThong.Format_SoDeHienThi(giaban, true) ;
650
651                     thanhT = giaban * soluong;
652
653                     dgvDsMuaHang.Rows[n].Cells[
"ThanhTien"].Value = CauHinhHeThong.Format_SoDeHienThi(thanhT, true);
654
655                     dgvDsMuaHang.Rows[n].Cells[
"trangthai2"].Value = item["LoaiHangHoa"].ToString();
656
657                     
658                     trangThai = item[
"LoaiHangHoa"].ToString();
659                     
if (trangThai == "MuaHang")
660                     {
661                         dgvDsMuaHang.Rows[n].Cells[
"TrangThai"].Value = "Mua hàng";
662                     }
663                     
else
664                     {
665                         dgvDsMuaHang.Rows[n].Cells[
"TrangThai"].Value = "Hàng khuyễn mãi";
666                     }
667
668                     dgvDsMuaHang.Rows[n].Cells[
"Xoa"].Value = "Xóa";
669                     dgvDsMuaHang.Rows[n].Cells[
"Sua"].Value = "Sửa";
670                 }
671
672                 
673             }
674
675
676            
677         }
678
679         
/*
680         
private void HienThiDanhSachKhuyenMai(int getIdBom)
681         {
682             dgvDsKhuyenMai.Rows.Clear();
683             //Ẩn cột đầu tiên
684             dgvDsKhuyenMai.RowHeadersVisible =
false;
685             //-https://stackoverflow.com/questions/
17404969/right-align-a-column-in-datagridview-doesnt-work
686             
this.dgvDsKhuyenMai.Columns["STTKm"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
687             
this.dgvDsKhuyenMai.Columns["STTKm"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
688             //
this.dgvDanhSachNhapHang.Columns["STT"].SortMode = DataGridViewColumnSortMode.NotSortable;
689
690             
this.dgvDsKhuyenMai.Columns["TenDonViTinhKm"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
691             
this.dgvDsKhuyenMai.Columns["TenDonViTinhKm"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
692
693             
this.dgvDsKhuyenMai.Columns["SoLuongKm"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
694             
this.dgvDsKhuyenMai.Columns["SoLuongKm"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
695
696             
this.dgvDsKhuyenMai.Columns["GiaVonKm"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
697             
this.dgvDsKhuyenMai.Columns["GiaVonKm"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
698
699             
this.dgvDsKhuyenMai.Columns["GiaBanKm"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
700             
this.dgvDsKhuyenMai.Columns["GiaBanKm"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
701
702
703             DataTable dtBomItem =
new DataTable();
704
705             //
select từ bảng bán hàng
706             dtBomItem = Controllers.BomItemCtrl.FillDataSet_getBomItem_SearchAll(_getIdBomItem, getIdBom, _IdKho,
"", _TrangThai, "HangKhuyenMai", _GiaBanSp, _SoLuongSp, _TenSanPham, _DonViTinhId, _TenDonViTinh).Tables[0];
707             
int _stt = 0;
708             
double soluong = 0;
709             
double giaban = 0;
710             
double giavon = 0;
711
712             
if (dtBomItem.Rows.Count > 0)
713             {
714                 
foreach (DataRow item in dtBomItem.Rows)
715                 {
716                     _stt +=
1;
717                     
int n = dgvDsKhuyenMai.Rows.Add();
718
719                     dgvDsKhuyenMai.Rows[n].Cells[
"IdBomItemKm"].Value = item["IdBomItem"].ToString();
720                     dgvDsKhuyenMai.Rows[n].Cells[
"STTKm"].Value = _stt.ToString();
721                     dgvDsKhuyenMai.Rows[n].Cells[
"ChuanMaVachKm"].Value = item["ChuanMaVach"].ToString();
722                     dgvDsKhuyenMai.Rows[n].Cells[
"TenSanPhamKm"].Value = item["TenSanPham"].ToString();
723
724                     dgvDsKhuyenMai.Rows[n].Cells[
"TenDonViTinhKm"].Value = item["TenDonViTinh"].ToString();
725
726                     soluong = CauHinhHeThong.Format_SoDeTinhToan(item[
"SoLuong"].ToString(),false);
727                     dgvDsKhuyenMai.Rows[n].Cells[
"SoLuongKm"].Value = CauHinhHeThong.Format_SoDeHienThi(soluong, false) ;
728
729                     giavon = CauHinhHeThong.Format_SoDeTinhToan(item[
"GiaNhap"].ToString(), false);
730
731                     dgvDsKhuyenMai.Rows[n].Cells[
"GiaVonKm"].Value =CauHinhHeThong.Format_SoDeHienThi(giavon, true) ;
732
733                     giaban = CauHinhHeThong.Format_SoDeTinhToan(item[
"GiaBan"].ToString(), false);
734                     dgvDsKhuyenMai.Rows[n].Cells[
"GiaBanKm"].Value =CauHinhHeThong.Format_SoDeHienThi(giaban,true) ;
735
736                     dgvDsKhuyenMai.Rows[n].Cells[
"XoaKm"].Value = "Xóa";
737                 }
738
739                 //dua thong tin form ve mac dinh
740                 
this.ClearInforFormSp();
741             }
742         }
743
744          */

745         
private void dgvDsMuaHang_CellClick(object sender, DataGridViewCellEventArgs e)
746         {
747             
/*
748             //-https://stackoverflow.com/questions/
1132471/how-to-reference-column-name-instead-of-e-columnindex-in-winform-datagridview-ev
749             
if (dgvDsMuaHang.Columns[e.ColumnIndex].Name == "ChuanMaVachMuaHang" && e.RowIndex != -1)
750             {
751                 //-https://stackoverflow.com/questions/
12762036/datagridview-cell-click-event
752                 
if (dgvDsMuaHang.CurrentCell != null && dgvDsMuaHang.CurrentCell.Value != null)
753                 {
754                     frmBom_FormThayDoiMvach frmBomMv =
new frmBom_FormThayDoiMvach();
755
756                     frmBomMv.ShowDialog();
757                 }
758             }
759
760             */

761
762             
//xoa
763             
if (dgvDsMuaHang.Columns[e.ColumnIndex].Name == "Xoa" && e.RowIndex != -1)
764             {
765                 
//-https://stackoverflow.com/questions/12762036/datagridview-cell-click-event
766                 
if (dgvDsMuaHang.CurrentCell != null && dgvDsMuaHang.CurrentCell.Value != null)
767                 {
768                     
int bomItemId = Int32.Parse(dgvDsMuaHang.Rows[e.RowIndex].Cells["IdBomItemMuaHang"].Value.ToString());
769
770                     DialogResult dr = MessageBox.Show(
"Bạn có chắc chắn xóa?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
771                     
if (dr == DialogResult.Yes)
772                     {
773                         Controllers.BomItemCtrl.DeleteBomItem(bomItemId);
774
775                         
this.HienThiDanhSachMuaHang(_getIdBom);
776
777                         
this.ClearAllFormSp();
778                     }
779                     
else
780                     {
781                         
return;
782                     }
783                 }
784             }
785
786             
//click update
787             
if (dgvDsMuaHang.Columns[e.ColumnIndex].Name == "Sua" && e.RowIndex != -1)
788             {
789                 
//-https://stackoverflow.com/questions/12762036/datagridview-cell-click-event
790                 
if (dgvDsMuaHang.CurrentCell != null && dgvDsMuaHang.CurrentCell.Value != null)
791                 {
792
793                     txtChuanMaVach.Text = dgvDsMuaHang.Rows[e.RowIndex].Cells[
"ChuanMaVachMuaHang"].Value.ToString();
794                     txtSoLuongSp.Text = dgvDsMuaHang.Rows[e.RowIndex].Cells[
"SoLuongMuaHang"].Value.ToString();
795                     txtGiaBan.Text = dgvDsMuaHang.Rows[e.RowIndex].Cells[
"GiaBanMuaHang"].Value.ToString();
796                     txtTenSp.Text = dgvDsMuaHang.Rows[e.RowIndex].Cells[
"TenSanPhamMuaHang"].Value.ToString();
797
798                     
if (dgvDsMuaHang.Rows[e.RowIndex].Cells["trangthai2"].Value.ToString() == "MuaHang")
799                     {
800                         rdoMuaHang.Checked =
true;
801                     }
802                     
else
803                     {
804                         rdoHangKhuyenMai.Checked =
true;
805                     }
806
807                     _TenDonViTinh = dgvDsMuaHang.Rows[e.RowIndex].Cells[
"TenDonViTinhMuaHang"].Value.ToString();
808                     _DonViTinhId = Int32.Parse(dgvDsMuaHang.Rows[e.RowIndex].Cells[
"DonViTinhId"].Value.ToString());
809
810                     _getIdBomItem = Int32.Parse(dgvDsMuaHang.Rows[e.RowIndex].Cells[
"IdBomItemMuaHang"].Value.ToString());
811
812                     
//cho phep lưu
813                     btnLuu.Enabled =
true;
814                     nhanEnter =
true;
815                 }
816             }
817
818         }
819
820         
/*
821         
private void dgvDsKhuyenMai_CellClick(object sender, DataGridViewCellEventArgs e)
822         {
823             //xoa
824             
if (dgvDsKhuyenMai.Columns[e.ColumnIndex].Name == "XoaKm" && e.RowIndex != -1)
825             {
826                 //-https://stackoverflow.com/questions/
12762036/datagridview-cell-click-event
827                 
if (dgvDsKhuyenMai.CurrentCell != null && dgvDsKhuyenMai.CurrentCell.Value != null)
828                 {
829                     
int bomItemId = Int32.Parse(dgvDsKhuyenMai.Rows[e.RowIndex].Cells["IdBomItemKm"].Value.ToString());
830                     Controllers.BomItemCtrl.DeleteBomItem(bomItemId);
831
832                     
this.HienThiDanhSachKhuyenMai(_getIdBom);
833                 }
834             }
835
836             //click update
837             
if (dgvDsKhuyenMai.Columns[e.ColumnIndex].Name == "TenSanPhamKm" && e.RowIndex != -1)
838             {
839                 //-https://stackoverflow.com/questions/
12762036/datagridview-cell-click-event
840                 
if (dgvDsKhuyenMai.CurrentCell != null && dgvDsKhuyenMai.CurrentCell.Value != null)
841                 {
842
843                     txtChuanMaVach.Text = dgvDsKhuyenMai.Rows[e.RowIndex].Cells[
"ChuanMaVachKm"].Value.ToString();
844                     txtSoLuongSp.Text = dgvDsKhuyenMai.Rows[e.RowIndex].Cells[
"SoLuongKm"].Value.ToString();
845                     txtGiaBan.Text = dgvDsKhuyenMai.Rows[e.RowIndex].Cells[
"GiaBanKm"].Value.ToString();
846
847                     rdoHangKhuyenMai.Checked =
true;
848
849                     _getIdBomItem = Int32.Parse(dgvDsKhuyenMai.Rows[e.RowIndex].Cells[
"IdBomItemKm"].Value.ToString());
850                 }
851             }
852         }
853
854
855         */

856
857
858         
public void HienThiChiTietBom(int IdBom)
859         {
860             _getIdBom = IdBom;
861
862             
this.HienThiBom(_getIdBom);
863             
this.HienThiDanhSachMuaHang(_getIdBom);
864             
//this.HienThiDanhSachKhuyenMai(_getIdBom);
865         }
866
867         
private void HienThiBom(int _getIdBom)
868         {
869             DataTable dtBom =
new DataTable();
870             dtBom = Controllers.BomCtrl.FillDataSet_getBom_ByIdBom(_getIdBom).Tables[
0];
871
872             
if (dtBom.Rows.Count > 0)
873             {
874                 
foreach (DataRow item in dtBom.Rows)
875                 {
876                     txtTieuDe.Text = item[
"TieuDe"].ToString();
877                     txtSoLuongGoi.Text = item[
"SoLuong"].ToString();
878                     txtMoTa.Text = item[
"MoTa"].ToString();
879
880                     dateNgayBatDau.Value = DateTime.Parse(item[
"NgayBatDau"].ToString());
881
882                     dateNgayKetThuc.Value = DateTime.Parse(item[
"NgayKetThuc"].ToString());
883                 }
884             }
885         }
886
887         
private void linkLabel_Quaylai_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
888         {
889             
if (this.Link_QuayLai != null)
890             {
891                 
this.Link_QuayLai(sender, e);
892
893                 _getIdBom =
0;
894                 _getIdBomItem =
0;
895             }
896         }
897
898         
public void formatUctBomForm_Default()
899         {
900             _getIdBom =
0;
901             _getIdBomItem =
0;
902             
this.ClearAllFormSp();
903             txtTieuDe.Text =
"";
904             txtSoLuongGoi.Text =
"";
905             txtMoTa.Text =
"";
906             
this.HienThiChiTietBom(0);
907
908         }
909     }
910 }


Gõ tìm kiếm nhanh...